package com.health.tools;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.KeyEvent;

import java.io.File;

import io.reactivex.android.schedulers.AndroidSchedulers;
import rxhttp.RxHttp;

/**
 * author : xiaoxin
 * date   : 2021/4/9 16:05
 * desc   :
 * update :
 */
public class NovaAppUtils {

    private static final String TAG = "NovaAppUtils";

    public static void downLoadApk(Context context,String apkName,String apkUrl,String apkFilePath) {
        final ProgressDialog dialog = new ProgressDialog(context);
        dialog.setTitle("提示");
        dialog.setMessage("正在下载" + apkName + "请稍等");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0;
            }
        };

        dialog.setOnKeyListener(keylistener);
        dialog.setCancelable(false);

        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
//        String path = Environment.getExternalStorageDirectory() + "/Download/health/camera360.apk";
        RxHttp.get(apkUrl)
                .asDownload(apkFilePath, AndroidSchedulers.mainThread(), progress -> {
                    //下载进度回调,0-100，仅在进度有更新时才会回调，最多回调101次，最后一次回调文件存储路径
                    int currentProgress = progress.getProgress(); //当前进度 0-100
                    //下载进度
                    Log.e(TAG, "downLoadApk: " + currentProgress);
                    dialog.setProgress(currentProgress);
                }) //指定回调(进度/成功/失败)线程,不指定,默认在请求所在线程回调
                .subscribe(s -> {                  //s为String类型，这里为文件存储路径
                    //下载完成
                    File file = new File(apkFilePath);
                    Log.e("t", "file--" + file);
                    openFile(context,file);
                    dialog.dismiss();
                }, throwable -> {
                    //下载失败
                    dialog.setMessage("下载失败");
                });
    }

    //打开APK程序代码
    private static void openFile(Context context, File file) {
        //Android 7.0及以上
        if (Build.VERSION.SDK_INT > 24) {
            Uri apkUri = FileProvider.getUriForFile(context, "com.health.fileprovider", file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            context.startActivity(intent);
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            context.startActivity(intent);
        }
    }

}
